//
//  KATSprite.m
//  KATFramework
//
//  Created by Kantice on 16/3/23.
//  Copyright © 2016年 KatApp. All rights reserved.
//

#import "KATSprite.h"
#import "KATMath.h"

NSString * const kSpriteKeyType=@"sprite_key_type";
NSString * const kSpriteKeyMode=@"sprite_key_mode";
NSString * const kSpriteKeyModeGroup=@"sprite_key_mode_group";
NSString * const kSpriteKeyPosition=@"sprite_key_position";
NSString * const kSpriteKeyRotationX=@"sprite_key_rotation_x";
NSString * const kSpriteKeyRotationY=@"sprite_key_rotation_y";
NSString * const kSpriteKeyRotationZ=@"sprite_key_rotation_z";
NSString * const kSpriteKeyScaleX=@"sprite_key_scale_x";
NSString * const kSpriteKeyScaleY=@"sprite_key_scale_y";
NSString * const kSpriteKeyScaleZ=@"sprite_key_scale_z";
NSString * const kSpriteKeyScaleXY=@"sprite_key_scale_xy";
NSString * const kSpriteKeyContents=@"sprite_key_contents";
NSString * const kSpriteKey3DTransform=@"sprite_key_3d_transform";
NSString * const kSpriteKeyTransition=@"sprite_key_transition";
NSString * const kSpriteKeyOpacity=@"sprite_key_opacity";
NSString * const kSpriteKeyBgColor=@"sprite_key_bg_color";
NSString * const kSpriteKeyCornerRadius=@"sprite_key_corner_radius";
NSString * const kSpriteKeyBorderWidth=@"sprite_key_border_width";
NSString * const kSpriteKeyBorderColor=@"sprite_key_border_color";
NSString * const kSpriteKeyName=@"sprite_key_name";
NSString * const kSpriteDefaultName=@"sprite_default_name";
NSString * const kSpriteModePosition=@"sprite_mode_position";
NSString * const kSpriteModeRotationX=@"sprite_mode_rotation_x";
NSString * const kSpriteModeRotationY=@"sprite_mode_rotation_y";
NSString * const kSpriteModeRotationZ=@"sprite_mode_rotation_z";
NSString * const kSpriteModeScaleX=@"sprite_mode_scale_x";
NSString * const kSpriteModeScaleY=@"sprite_mode_scale_y";
NSString * const kSpriteModeScaleZ=@"sprite_mode_scale_z";
NSString * const kSpriteModeScaleXY=@"sprite_mode_scale_xy";
NSString * const kSpriteModeContents=@"sprite_mode_contents";
NSString * const kSpriteMode3DTransform=@"sprite_mode_3d_transform";
NSString * const kSpriteModeOpacity=@"sprite_mode_opacity";
NSString * const kSpriteModeBgColor=@"sprite_mode_bg_color";
NSString * const kSpriteModeCornerRadius=@"sprite_mode_corner_radius";
NSString * const kSpriteModeBorderWidth=@"sprite_mode_border_width";
NSString * const kSpriteModeBorderColor=@"sprite_mode_border_color";

NSString * const kSpriteTypeBasic=@"sprite_type_basic";
NSString * const kSpriteTypeKeyFrame=@"sprite_type_key_frame";
NSString * const kSpriteTypePath=@"sprite_type_path";
NSString * const kSpriteTypeGroup=@"sprite_type_group";
NSString * const kSpriteTypeTransition=@"sprite_type_transition";

NSString * const kSpriteAnimationHeartbeat=@"sprite_animation_heartbeat";
NSString * const kSpriteAnimationBlink=@"sprite_animation_blink";
NSString * const kSpriteAnimationRotate=@"sprite_animation_rotate";
NSString * const kSpriteAnimationFlip=@"sprite_animation_flip";
NSString * const kSpriteAnimationShake=@"sprite_animation_shake";
NSString * const kSpriteAnimationShock=@"sprite_animation_shock";
NSString * const kSpriteAnimationDither=@"sprite_animation_dither";
NSString * const kSpriteAnimationDisappear=@"sprite_animation_disappear";
NSString * const kSpriteAnimationAppear=@"sprite_animation_appear";
NSString * const kSpriteAnimationScaledDisappear=@"sprite_animation_scaled_disappear";
NSString * const kSpriteAnimationScaledAppear=@"sprite_animation_scaled_appear";
NSString * const kSpriteAnimationZoomIn=@"sprite_animation_zoom_in";
NSString * const kSpriteAnimationZoomOut=@"sprite_animation_zoom_out";
NSString * const kSpriteAnimationRoll=@"sprite_animation_roll";
NSString * const kSpriteAnimationRollIn=@"sprite_animation_roll_in";
NSString * const kSpriteAnimationRollOut=@"sprite_animation_roll_out";
NSString * const kSpriteAnimationRotateIn=@"sprite_animation_rotate_in";
NSString * const kSpriteAnimationRotateOut=@"sprite_animation_rotate_out";
NSString * const kSpriteAnimationDrop=@"sprite_animation_drop";
NSString * const kSpriteAnimationFloat=@"sprite_animation_float";
NSString * const kSpriteAnimationMoveQuickly=@"sprite_animation_move_quickly";
NSString * const kSpriteAnimationMove=@"sprite_animation_come_in";
NSString * const kSpriteAnimationComeIn=@"sprite_animation_go_out";
NSString * const kSpriteAnimationGoOut=@"sprite_animation_move";
NSString * const kSpriteAnimationJump=@"sprite_animation_jump";



@interface KATSprite ()

///原始Frame
@property(nonatomic,assign) CGRect originalFrame;

///是否被调整过尺寸
@property(nonatomic,assign) BOOL isResized;

@end




@implementation KATSprite



+ (KATSprite *)sprite
{
    KATSprite *sprite=[[[self alloc] init] autorelease];
        
    return sprite;
}


///初始化精灵
- (instancetype)init
{
    self=[super init];
    
    self.animating=NO;
    self.interactInAnimating=YES;
    self.restoreAfterAnimating=NO;
    self.keepStateOnStop=YES;
    self.animationStoped=NO;
    self.contentMode=UIViewContentModeScaleAspectFit;
    self.userInteractionEnabled=YES;
    self.backgroundColor=[UIColor clearColor];
    self.layer.borderColor=[UIColor clearColor].CGColor;
    self.layer.borderWidth=0.0f;
    self.layer.cornerRadius=0.0f;
    self.angleX=0.0f;
    self.angleY=0.0f;
    self.angleZ=0.0f;
    self.sizeX=1.0f;
    self.sizeY=1.0f;
    self.sizeZ=1.0f;
    self.sizeXY=1.0f;
    self.perspective=-0.002;
    self.contents=nil;
    self.animation=nil;
    self.spriteDelegate=nil;
    self.animationCalculationMode=kCAAnimationLinear;
    self.animationFillMode=kCAFillModeRemoved;
    self.animationRotateMode=nil;
    self.animationTimingMode=kCAMediaTimingFunctionDefault;
    self.displayLinkTimer=nil;
    self.displayLinkActionsRunning=NO;
    self.displayLinkActions=nil;
    self.animationQueue=[KATQueue queueWithCapacity:16];
    self.isAutoResize=NO;
    self.resizeType=KATSpriteResizeTypeDefault;
    self.isResized=NO;
    
    return self;
}


//获取当前展示图层
- (CALayer *)presentLayer
{
    CALayer *present=(CALayer *)self.layer.presentationLayer;
    
    if(present)
    {
        return present;
    }
    else
    {
        return self.layer;
    }
}


//设置frame
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    _isResized=NO;
}


//设置图片
- (void)setContents:(UIImage *)contents
{
    if(contents!=_contents)
    {
        [_contents release];
        
        _contents=[contents retain];
        
        if([NSThread currentThread]!=[NSThread mainThread])//非主线程
        {
            GCD_SYNC_MAIN(
            {
                self.layer.contents=(__bridge id)_contents.CGImage;
                
                //回调
                if(_spriteDelegate && [_spriteDelegate respondsToSelector:@selector(spriteDidChangeContent:)])
                {
                    [_spriteDelegate spriteDidChangeContent:self];
                }
                
                //自适应
                if(_isAutoResize)
                {
                    [self resizeForContents];
                }
            });
        }
        else//主线程
        {
            self.layer.contents=(__bridge id)_contents.CGImage;
            
            //回调
            if(_spriteDelegate && [_spriteDelegate respondsToSelector:@selector(spriteDidChangeContent:)])
            {
                [_spriteDelegate spriteDidChangeContent:self];
            }
            
            //自适应
            if(_isAutoResize)
            {
                [self resizeForContents];
            }
        }
    }
}


//获取转场动画
+ (CATransition *)transitionWithType:(NSString *)type subtype:(NSString *)subtype duration:(double)duration delay:(float)delay
{
    if(!type)
    {
        return nil;
    }
    
    //创建转场动画对象
    CATransition *transition=[[[CATransition alloc]init] autorelease];
    
    transition.type=type;
    
    if(subtype)
    {
        transition.subtype=subtype;
    }
    
    transition.duration=duration;
    
    //携带值
    [transition setValue:kSpriteTypeTransition forKey:kSpriteKeyType];//类型
    [transition setValue:type forKey:kSpriteKeyTransition];//值
    
    return transition;
}


#pragma -mark 获取动画

//获取位移动画
- (CABasicAnimation *)animationToPosition:(CGPoint)position withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"position"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSValue valueWithCGPoint:self.layer.position];
    animation.toValue=[NSValue valueWithCGPoint:position];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModePosition forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSValue valueWithCGPoint:position] forKey:kSpriteKeyPosition];//值
    
    return animation;
}


///获取不透明度动画
- (CABasicAnimation *)animationToOpacity:(float)opacity withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.layer.opacity];
    animation.toValue=[NSNumber numberWithFloat:opacity];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeOpacity forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:opacity] forKey:kSpriteKeyOpacity];//值
    
    
    return animation;
}


//获取背景色动画
- (CABasicAnimation *)animationBgToColor:(UIColor *)color withDuration:(double)duration repeat:(float)repeat delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"backgroundColor"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=self.backgroundColor;
    animation.toValue=color;
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=NO;//是否回到原来的位置(颜色恢复原来则不会有动画)
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeBgColor forKey:kSpriteKeyMode];//模式
    [animation setValue:color forKey:kSpriteKeyBgColor];//值
    
    
    return animation;
}



//获取边线色动画
- (CABasicAnimation *)animationBorderToColor:(UIColor *)color withDuration:(double)duration repeat:(float)repeat delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"borderColor"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[UIColor colorWithCGColor:self.layer.borderColor];
    animation.toValue=color;
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=NO;//是否回到原来的位置(颜色恢复原来则不会有动画)
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeBorderColor forKey:kSpriteKeyMode];//模式
    [animation setValue:color forKey:kSpriteKeyBorderColor];//值
    
    
    return animation;
}


//获取边线宽动画
- (CABasicAnimation *)animationBorderToWidth:(float)width withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"borderWidth"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.layer.borderWidth];
    animation.toValue=[NSNumber numberWithFloat:width];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeBorderWidth forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:width] forKey:kSpriteKeyBorderWidth];//值
    
    
    return animation;
}


//获取圆角半径动画
- (CABasicAnimation *)animationCornerToRadius:(float)radius withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"cornerRadius"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.layer.cornerRadius];
    animation.toValue=[NSNumber numberWithFloat:radius];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeCornerRadius forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:radius] forKey:kSpriteKeyCornerRadius];//值
    
    
    return animation;
}


//获取X轴旋转动画
- (CABasicAnimation *)animationRotationXToAngle:(float)angle withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.angleX];
    animation.toValue=[NSNumber numberWithFloat:angle];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeRotationX forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:angle] forKey:kSpriteKeyRotationX];//值
    [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,angle-self.angleX, 1.0, 0.0, 0.0)] forKey:kSpriteKey3DTransform];//值
    
    
    return animation;
}


//获取Y轴旋转动画
- (CABasicAnimation *)animationRotationYToAngle:(float)angle withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.angleY];
    animation.toValue=[NSNumber numberWithFloat:angle];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeRotationY forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:angle] forKey:kSpriteKeyRotationY];//值
    [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,angle-self.angleY, 0.0, 1.0, 0.0)] forKey:kSpriteKey3DTransform];//值
    
    
    return animation;
}


//获取Z轴旋转动画
- (CABasicAnimation *)animationRotationZToAngle:(float)angle withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.angleZ];
    animation.toValue=[NSNumber numberWithFloat:angle];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeRotationZ forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:angle] forKey:kSpriteKeyRotationZ];//值
    [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,angle-self.angleZ, 0.0, 0.0, 1.0)] forKey:kSpriteKey3DTransform];//值
    
    
    return animation;
}


//获取X轴缩放动画
- (CABasicAnimation *)animationScaleXToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeX];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeScaleX forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kSpriteKeyScaleX];//值
    
    if(self.sizeX!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, size/self.sizeX, 1.0, 1.0)] forKey:kSpriteKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(size, 1.0, 1.0)] forKey:kSpriteKey3DTransform];//值
    }
    
    
    return animation;
}


//获取Y轴缩放动画
- (CABasicAnimation *)animationScaleYToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeY];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeScaleY forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kSpriteKeyScaleY];//值
    
    if(self.sizeY!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, 1.0, size/self.sizeY, 1.0)] forKey:kSpriteKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, size, 1.0)] forKey:kSpriteKey3DTransform];//值
    }
    
    
    return animation;
}


//获取Z轴缩放动画
- (CABasicAnimation *)animationScaleZToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale.z"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeZ];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeScaleZ forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kSpriteKeyScaleZ];//值
    
    if(self.sizeZ!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, 1.0, 1.0, size/self.sizeZ)] forKey:kSpriteKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, size)] forKey:kSpriteKey3DTransform];//值
    }
    
    
    return animation;
}


//获取XY轴(等比例)缩放动画
- (CABasicAnimation *)animationScaleXYToSize:(float)size withDuration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    //创建动画并指定动画属性
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.scale"];
    
    //设置动画属性初始值和结束值
    animation.fromValue=[NSNumber numberWithFloat:self.sizeXY];
    animation.toValue=[NSNumber numberWithFloat:size];
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeBasic forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModeScaleXY forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSNumber numberWithFloat:size] forKey:kSpriteKeyScaleXY];//值
    
    if(self.sizeXY!=0.0)
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, size/self.sizeXY*self.sizeY/self.sizeXY, size/self.sizeXY*self.sizeX/self.sizeXY, 1.0)] forKey:kSpriteKey3DTransform];//值
    }
    else
    {
        [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(size, size, 1.0)] forKey:kSpriteKey3DTransform];//值
    }
    
    return animation;
}


//获取路径动画(特殊的位移关键帧动画，优先级高于位移关键帧)
- (CAKeyframeAnimation *)animationWithPath:(UIBezierPath *)path duration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    if(!path)
    {
        return nil;
    }
    
    //创建关键帧动画并设置动画属性
    CAKeyframeAnimation *animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //设置动画路径
    animation.path=path.CGPath;
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    animation.calculationMode=self.animationCalculationMode;//计算模式
    animation.fillMode=self.animationFillMode;//填充模式
    animation.rotationMode=self.animationRotateMode;//旋转模式
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypePath forKey:kSpriteKeyType];//类型
    [animation setValue:kSpriteModePosition forKey:kSpriteKeyMode];//模式
    [animation setValue:[NSValue valueWithCGPoint:path.currentPoint] forKey:kSpriteKeyPosition];//值
    
    
    return animation;
}


//获取关键帧动画
- (CAKeyframeAnimation *)animationWithMode:(int)mode keyValues:(NSArray *)values keyTimes:(NSArray *)times duration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    if(!values)
    {
        return nil;
    }
    
    //创建关键帧动画并设置动画属性
    CAKeyframeAnimation *animation=nil;
    
    switch(mode)
    {
        case SPRITE_MODE_POSITION://位移模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"position"];
            
            //设置携带值
            [animation setValue:kSpriteModePosition forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyPosition];//值
            
            break;
            
            
        case SPRITE_MODE_OPACITY://透明模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"opacity"];
            
            //设置携带值
            [animation setValue:kSpriteModeOpacity forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyOpacity];//值
            
            break;
            
            
        case SPRITE_MODE_BG_COLOR://背景色模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
            
            //设置携带值
            [animation setValue:kSpriteModeBgColor forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyBgColor];//值
            
            //颜色对象转化
            NSMutableArray *bgColors=[NSMutableArray array];
            
            for(UIColor *color in values)
            {
                if([color isKindOfClass:[UIColor class]])
                {
                    [bgColors addObject:(__bridge id)color.CGColor];
                }
            }
            
            values=bgColors;
            
            break;
            
            
        case SPRITE_MODE_BORDER_COLOR://边线色模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"borderColor"];
            
            //设置携带值
            [animation setValue:kSpriteModeBorderColor forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyBorderColor];//值
            
            //颜色对象转化
            NSMutableArray *borderColors=[NSMutableArray array];
            
            for(UIColor *color in values)
            {
                if([color isKindOfClass:[UIColor class]])
                {
                    [borderColors addObject:(__bridge id)color.CGColor];
                }
            }
            
            values=borderColors;
            
            break;
            
            
        case SPRITE_MODE_BORDER_WIDTH://边线宽模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"borderWidth"];
            
            //设置携带值
            [animation setValue:kSpriteModeBorderWidth forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyBorderWidth];//值
            
            break;
            
            
        case SPRITE_MODE_CORNER_RADIUS://圆角模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"cornerRadius"];
            
            //设置携带值
            [animation setValue:kSpriteModeCornerRadius forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyCornerRadius];//值
            
            break;
            
            
        case SPRITE_MODE_ROTATION_X://X轴旋转模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.x"];
            
            //设置携带值
            [animation setValue:kSpriteModeRotationX forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyRotationX];//值
            [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,[[values lastObject] floatValue]-self.angleX, 1.0, 0.0, 0.0)] forKey:kSpriteKey3DTransform];//值
            
            break;
            
            
        case SPRITE_MODE_ROTATION_Y://Y轴旋转模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.y"];
            
            //设置携带值
            [animation setValue:kSpriteModeRotationY forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyRotationY];//值
            [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,[[values lastObject] floatValue]-self.angleY, 0.0, 1.0, 0.0)] forKey:kSpriteKey3DTransform];//值
            
            break;
            
            
        case SPRITE_MODE_ROTATION_Z://Z轴旋转模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
            
            //设置携带值
            [animation setValue:kSpriteModeRotationZ forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyRotationZ];//值
            [animation setValue:[NSValue valueWithCATransform3D:CATransform3DRotate(self.layer.transform,[[values lastObject] floatValue]-self.angleZ, 0.0, 0.0, 1.0)] forKey:kSpriteKey3DTransform];//值
            
            break;
            
            
        case SPRITE_MODE_SCALE_X://X轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"];
            
            //设置携带值
            [animation setValue:kSpriteModeScaleX forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyScaleX];//值
            
            if(self.sizeX!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, [[values lastObject] floatValue]/self.sizeX, 1.0, 1.0)] forKey:kSpriteKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale([[values lastObject] floatValue], 1.0, 1.0)] forKey:kSpriteKey3DTransform];//值
            }
            
            break;
            
            
        case SPRITE_MODE_SCALE_Y://Y轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.y"];
            
            //设置携带值
            [animation setValue:kSpriteModeScaleY forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyScaleY];//值
            
            if(self.sizeY!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, 1.0, [[values lastObject] floatValue]/self.sizeY, 1.0)] forKey:kSpriteKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, [[values lastObject] floatValue], 1.0)] forKey:kSpriteKey3DTransform];//值
            }
            
            break;
            
            
        case SPRITE_MODE_SCALE_Z://Z轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale.z"];
            
            //设置携带值
            [animation setValue:kSpriteModeScaleZ forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyScaleZ];//值
            
            if(self.sizeZ!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, 1.0, 1.0, [[values lastObject] floatValue]/self.sizeZ)] forKey:kSpriteKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, [[values lastObject] floatValue])] forKey:kSpriteKey3DTransform];//值
            }
            
            break;
            
            
        case SPRITE_MODE_SCALE_XY://XY轴缩放模式
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            
            //设置携带值
            [animation setValue:kSpriteModeScaleXY forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyScaleXY];//值
            
            if(self.sizeXY!=0.0)
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DScale(self.layer.transform, [[values lastObject] floatValue]/self.sizeXY*self.sizeY/self.sizeXY, [[values lastObject] floatValue]/self.sizeXY*self.sizeX/self.sizeXY, 1.0)] forKey:kSpriteKey3DTransform];//值
            }
            else
            {
                [animation setValue:[NSValue valueWithCATransform3D:CATransform3DMakeScale([[values lastObject] floatValue], [[values lastObject] floatValue], 1.0)] forKey:kSpriteKey3DTransform];//值
            }
            
            break;
            
            
        case SPRITE_MODE_CONTENTS://内容变换模式
            
            //保存原始值
            if(self.layer.contents && _contents!=self.layer.contents)
            {
                [_contents release];
                _contents=[self.layer.contents retain];
            }
            
            animation=[CAKeyframeAnimation animationWithKeyPath:@"contents"];
            
            //设置携带值
            [animation setValue:kSpriteModeContents forKey:kSpriteKeyMode];//模式
            [animation setValue:[values lastObject] forKey:kSpriteKeyContents];//值
            
            //图片对象转化
            NSMutableArray *images=[NSMutableArray array];
            
            for(UIImage *image in values)
            {
                if([image isKindOfClass:[UIImage class]])
                {
                    [images addObject:(__bridge id)image.CGImage];
                }
            }
            
            values=images;
            
            break;
            
            
        default:
            
            return nil;
    }
    
    
    //设置动画关键值和时间
    animation.values=values;
    
    if(times)
    {
        animation.keyTimes=times;
    }
    
    //设置其他动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    animation.calculationMode=self.animationCalculationMode;//计算模式
    animation.fillMode=self.animationFillMode;//填充模式
    animation.rotationMode=self.animationRotateMode;//旋转模式
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    //设置携带值
    [animation setValue:kSpriteTypeKeyFrame forKey:kSpriteKeyType];//类型
    
    return animation;
}


///获取动画组
- (CAAnimationGroup *)animationWithGroup:(NSArray *)group duration:(double)duration repeat:(float)repeat autoreverses:(BOOL)autoreverses delay:(float)delay
{
    if(!group)
    {
        return nil;
    }
    
    //创建动画组
    CAAnimationGroup *animation=[CAAnimationGroup animation];
    
    //设置携带值
    [animation setValue:kSpriteTypeGroup forKey:kSpriteKeyType];//类型
    
    CATransform3D transform=self.layer.transform;//形变变量
    NSMutableArray *array=[[[NSMutableArray alloc] init] autorelease];//最终放入动画组的数组（过滤重复模式）
    
    for(int i=0;i<group.count;i++)
    {
        CAAnimation *an=[group objectAtIndex:i];
        
        if(an && [an isKindOfClass:[CAAnimation class]])//判断类型
        {
            an.beginTime=an.beginTime-CACurrentMediaTime();//动画时间调整
            
            NSString *mode=[an valueForKey:kSpriteKeyMode];
            NSString *key=nil;
            
            if([kSpriteModePosition isEqualToString:mode])//位移模式
            {
                key=kSpriteKeyPosition;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeOpacity isEqualToString:mode])//透明模式
            {
                key=kSpriteKeyOpacity;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeBgColor isEqualToString:mode])//背景色模式
            {
                key=kSpriteKeyBgColor;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeBorderColor isEqualToString:mode])//边线色模式
            {
                key=kSpriteKeyBorderColor;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeBorderWidth isEqualToString:mode])//边线宽模式
            {
                key=kSpriteKeyBorderWidth;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeCornerRadius isEqualToString:mode])//圆角模式
            {
                key=kSpriteKeyCornerRadius;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeRotationX isEqualToString:mode])//X旋转模式
            {
                key=kSpriteKeyRotationX;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    transform=CATransform3DRotate(transform,[[an valueForKey:key] floatValue]-self.angleX, 1.0, 0.0, 0.0);//形变
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeRotationY isEqualToString:mode])//Y旋转模式
            {
                key=kSpriteKeyRotationY;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    transform=CATransform3DRotate(transform,[[an valueForKey:key] floatValue]-self.angleY, 0.0, 1.0, 0.0);//形变
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeRotationZ isEqualToString:mode])//Y旋转模式
            {
                key=kSpriteKeyRotationZ;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    transform=CATransform3DRotate(transform,[[an valueForKey:key] floatValue]-self.angleZ, 0.0, 0.0, 1.0);//形变
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeScaleX isEqualToString:mode])//X缩放模式
            {
                key=kSpriteKeyScaleX;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeX!=0.0)
                    {
                        transform=CATransform3DScale(transform, [[an valueForKey:key] floatValue]/self.sizeX, 1.0, 1.0);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale([[an valueForKey:key] floatValue], 1.0, 1.0);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeScaleY isEqualToString:mode])//Y缩放模式
            {
                key=kSpriteKeyScaleY;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeY!=0.0)
                    {
                        transform=CATransform3DScale(transform, 1.0, [[an valueForKey:key] floatValue]/self.sizeY, 1.0);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale(1.0, [[an valueForKey:key] floatValue], 1.0);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeScaleZ isEqualToString:mode])//Z缩放模式
            {
                key=kSpriteKeyScaleZ;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeZ!=0.0)
                    {
                        transform=CATransform3DScale(transform, 1.0, 1.0, [[an valueForKey:key] floatValue]/self.sizeZ);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale(1.0, 1.0, [[an valueForKey:key] floatValue]);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeScaleXY isEqualToString:mode])//XY缩放模式
            {
                key=kSpriteKeyScaleXY;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    if(self.sizeXY!=0.0)
                    {
                        transform=CATransform3DScale(transform, [[an valueForKey:key] floatValue]/self.sizeXY*self.sizeY/self.sizeXY, [[an valueForKey:key] floatValue]/self.sizeXY*self.sizeX/self.sizeXY, 1.0);//形变
                    }
                    else
                    {
                        transform=CATransform3DMakeScale([[an valueForKey:key] floatValue], [[an valueForKey:key] floatValue], 1.0);//形变
                    }
                    
                    [array addObject:an];
                }
            }
            else if([kSpriteModeContents isEqualToString:mode])//内容模式
            {
                key=kSpriteKeyContents;
                
                if(![animation valueForKey:key])//没有该值则存入
                {
                    [animation setValue:[an valueForKey:key] forKey:key];//值
                    
                    [array addObject:an];
                }
            }
        }
    }
    
    
    [animation setValue:[NSValue valueWithCATransform3D:transform] forKey:kSpriteKey3DTransform];//设置形变值
    
    animation.animations=array;//设置动画组
    
    //设置动画属性
    animation.duration=duration;//动画时间
    animation.repeatCount=repeat<0?HUGE_VALF:repeat;//设置重复次数,HUGE_VALF可看做无穷大，起到循环动画的效果
    animation.autoreverses=autoreverses;//是否回到原来的位置
    animation.beginTime=CACurrentMediaTime()+delay;//延迟时间
    animation.fillMode=self.animationFillMode;//填充模式
    
    if(self.animationTimingMode)
    {
        animation.timingFunction=[CAMediaTimingFunction functionWithName:self.animationTimingMode];//加速模式
    }
    
    return animation;
}


#pragma -mark 动画执行

//开始动画(设置动画名字)
- (void)startAnimation:(CAAnimation *)animation withName:(NSString *)name andDelay:(double)delay
{
    if(animation && name)
    {
        //防止循环引用
        animation=[[animation copy] autorelease];
        
        animation.delegate=self;
        animation.beginTime=CACurrentMediaTime()+delay;//设置开始时间
        
        [animation setValue:name forKey:kSpriteKeyName];//设置动画名称
        
        if(!_animating)//没有动画执行，则直接执行动画
        {
            if([kSpriteModePosition isEqualToString:[animation valueForKey:kSpriteKeyMode]])//包含位移动画，则在动画进行中取消交互
            {
                _interactInAnimating=NO;
            }
            
            _animating=YES;

            [self.layer addAnimation:animation forKey:name];
        }
        else//有动画在执行中，则放入队列
        {
            [self.animationQueue put:animation];
        }
    }
}


//开始动画(默认动画名字)
- (void)startAnimation:(CAAnimation *)animation andDelay:(double)delay
{
    if(animation)
    {
        if([animation valueForKey:kSpriteKeyName])//本身带名字的（队列里的动画）
        {
            [self startAnimation:animation withName:[animation valueForKey:kSpriteKeyName] andDelay:delay];
        }
        else
        {
            [self startAnimation:animation withName:kSpriteDefaultName andDelay:delay];
        }
    }
    
}


///暂停动画
- (void)pauseAnimation
{
    if(self.layer.speed>0)
    {
        //取得指定图层动画的媒体时间，后面参数用于指定子图层，这里不需要
        CFTimeInterval interval=[self.layer convertTime:CACurrentMediaTime() fromLayer:nil];
        
        //设置时间偏移量，保证暂停时停留在旋转的位置
        [self.layer setTimeOffset:interval];
        
        //速度设置为0，暂停动画
        self.layer.speed=0;
    }
}


///恢复动画
- (void)resumeAnimation
{
    if(self.layer.speed<1.0)
    {
        //获得暂停的时间
        CFTimeInterval beginTime=CACurrentMediaTime()-self.layer.timeOffset;
        
        //设置偏移量
        self.layer.timeOffset=0;
        
        //设置开始时间
        self.layer.beginTime=beginTime;
        
        //设置动画速度，开始运动
        self.layer.speed=1.0;
    }
}


//开始系统刷屏动作(interval表示每间隔几帧刷屏执行一次，1代表每帧执行一次，约为每秒60次),不能同时进行多个刷屏动作，新的会替换旧的
- (void)startDisplayLinkActions:(void (^)(void))actions withFrameInterval:(int)interval
{
    //先停止之前的刷屏动画
    [self stopDisplayLinkActions];
    
    if(!actions)
    {
        return;
    }
    
    //创建
    self.displayLinkTimer=[CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkActionsRun)];
    self.displayLinkTimer.frameInterval=interval;
    self.displayLinkActions=actions;
    
    //添加定时器对象到主运行循环
    //可以同时加入NSDefaultRunLoopMode和UITrackingRunLoopMode来保证它不会被滑动打断，也不会被其他UIKit控件动画影响性能
    [_displayLinkTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    [_displayLinkTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:UITrackingRunLoopMode];
    
    _displayLinkActionsRunning=YES;
}


//停止并移除系统刷屏动作
- (void)stopDisplayLinkActions
{
    if(_displayLinkTimer)
    {
        [_displayLinkTimer invalidate];
        [_displayLinkTimer release];
        
        _displayLinkTimer=nil;
        
        _displayLinkActionsRunning=NO;
        
        if(_displayLinkActions)
        {
            Block_release(_displayLinkActions);
            _displayLinkActions=nil;
        }
    }
}


//刷屏动作
- (void)displayLinkActionsRun
{
    if(_displayLinkActions)
    {
        _displayLinkActions();
    }
}



#pragma -mark 变形

//旋转
- (void)transformRotateToAngle:(float)angle
{
    self.layer.transform=CATransform3DRotate(CATransform3DIdentity, angle, 0.0, 0.0, 1.0);
    
    //保存变换角度
    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//旋转X轴（上下转）
- (void)transformRotateXToAngle:(float)angle
{
    CATransform3D transform=CATransform3DIdentity;
    
    transform.m34=_perspective;
    
    self.layer.transform=CATransform3DRotate(transform, angle, 1.0, 0.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//旋转Y轴（左右转）
- (void)transformRotateYToAngle:(float)angle
{
    CATransform3D transform=CATransform3DIdentity;
    
    transform.m34=_perspective;
    
    self.layer.transform=CATransform3DRotate(transform, angle, 0.0, 1.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//水平镜像
- (void)transformHorizontalMirror
{
    self.layer.transform=CATransform3DRotate(CATransform3DIdentity, M_PI, 1.0, 0.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//垂直镜像
- (void)transformVerticalMirror
{
    self.layer.transform=CATransform3DRotate(CATransform3DIdentity, M_PI, 0.0, 1.0, 0.0);
    
    //保存变换角度
    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//缩放(宽高)
- (void)transformScaleToWitdh:(float)width andHeight:(float)height
{
    self.layer.transform=CATransform3DScale(CATransform3DIdentity, width, height, 1.0);
    
    //保存变换角度
    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
    
    //保存变换尺寸
    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
    self.sizeXY=(_sizeX+_sizeY)/2.0;
}


//复位
- (void)restoreSprite
{
    //停止动画
    [self stopAnimation];
    
    self.layer.transform=CATransform3DIdentity;
    self.sizeX=1.0f;
    self.sizeY=1.0f;
    self.sizeZ=1.0f;
    self.sizeXY=1.0f;
    self.angleX=0.0f;
    self.angleY=0.0f;
    self.angleZ=0.0f;
    
    self.layer.opacity=1.0f;
    
    if(_contents)
    {
        self.layer.contents=(__bridge id)_contents.CGImage;
    }
}


//内容调整尺寸
- (void)resizeForContents
{
    if(_contents && _contents.size.width>0 && _contents.size.height>0)
    {
        if(!_isResized)//保存原始尺寸
        {
            _originalFrame=self.frame;
        }
        
        switch(_resizeType)
        {
            case KATSpriteResizeTypeDefault://左上角位置不变，不超过原来的尺寸
                
                {
                    double w=_contents.size.width/SCREEN_SCALE;
                    double h=_contents.size.height/SCREEN_SCALE;
                    double rate=h/w;//高宽比
                    CGSize fitSize;//修正后的尺寸
                    
                    //以小的为基准
                    if(_originalFrame.size.height/rate<=_originalFrame.size.width)//以高为基准
                    {
                        if(h>_originalFrame.size.height)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.height/rate, _originalFrame.size.height);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    else//以宽为基准
                    {
                        if(w>_originalFrame.size.width)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.width, _originalFrame.size.width*rate);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    
                    self.frame=RECT(_originalFrame.origin.x, _originalFrame.origin.y, fitSize.width, fitSize.height);
                }
                
                break;
                
                
            case KATSpriteResizeTypeCenter://中心位置不变，不超过原来的尺寸
                
                {
                    double w=_contents.size.width/SCREEN_SCALE;
                    double h=_contents.size.height/SCREEN_SCALE;
                    double rate=h/w;//高宽比
                    CGSize fitSize;//修正后的尺寸
                    
                    //以小的为基准
                    if(_originalFrame.size.height/rate<=_originalFrame.size.width)//以高为基准
                    {
                        if(h>_originalFrame.size.height)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.height/rate, _originalFrame.size.height);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    else//以宽为基准
                    {
                        if(w>_originalFrame.size.width)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.width, _originalFrame.size.width*rate);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    
                    self.bounds=RECT(0, 0, fitSize.width, fitSize.height);
                }
                
                break;
                
                
            case KATSpriteResizeTypeCenterHorizontal://水平方向中心位置不变，不超过原来的尺寸
                
                {
                    double w=_contents.size.width/SCREEN_SCALE;
                    double h=_contents.size.height/SCREEN_SCALE;
                    double rate=h/w;//高宽比
                    CGSize fitSize;//修正后的尺寸
                    
                    //以小的为基准
                    if(_originalFrame.size.height/rate<=_originalFrame.size.width)//以高为基准
                    {
                        if(h>_originalFrame.size.height)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.height/rate, _originalFrame.size.height);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    else//以宽为基准
                    {
                        if(w>_originalFrame.size.width)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.width, _originalFrame.size.width*rate);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    
                    self.frame=RECT(_originalFrame.origin.x+(_originalFrame.size.width-fitSize.width)/2.0, _originalFrame.origin.y, fitSize.width, fitSize.height);
                }
                
                break;
                
                
            case KATSpriteResizeTypeCenterVertical://垂直方向中心位置不变，不超过原来的尺寸
                
                {
                    double w=_contents.size.width/SCREEN_SCALE;
                    double h=_contents.size.height/SCREEN_SCALE;
                    double rate=h/w;//高宽比
                    CGSize fitSize;//修正后的尺寸
                    
                    //以小的为基准
                    if(_originalFrame.size.height/rate<=_originalFrame.size.width)//以高为基准
                    {
                        if(h>_originalFrame.size.height)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.height/rate, _originalFrame.size.height);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    else//以宽为基准
                    {
                        if(w>_originalFrame.size.width)
                        {
                            fitSize=CGSizeMake(_originalFrame.size.width, _originalFrame.size.width*rate);
                        }
                        else
                        {
                            fitSize=CGSizeMake(w, h);
                        }
                    }
                    
                    self.frame=RECT(_originalFrame.origin.x, _originalFrame.origin.y+(_originalFrame.size.height-fitSize.height)/2.0, fitSize.width, fitSize.height);
                }
                
                break;
                
                
            case KATSpriteResizeTypeOriginal://左上角位置不变，图片原始尺寸
                
                self.frame=RECT(_originalFrame.origin.x, _originalFrame.origin.y, _contents.size.width/SCREEN_SCALE, _contents.size.height/SCREEN_SCALE);
                
                break;
                
                
            case KATSpriteResizeTypeCenterOriginal://中心位置不变，图片原始尺寸
                
                self.bounds=RECT(0, 0, _contents.size.width/SCREEN_SCALE, _contents.size.height/SCREEN_SCALE);
                
                break;
                
                
            case KATSpriteResizeTypeNone://不变
            default:
                
                break;
        }
        
        _isResized=YES;
        
        //回调
        if(_spriteDelegate && [_spriteDelegate respondsToSelector:@selector(spriteDidResize:)])
        {
            [_spriteDelegate spriteDidResize:self];
        }
    }
}


#pragma -mark 常用动画

//心跳(等比缩放)(参数小于0则用默认值)
- (void)heartbeatWithScale:(float)scale duration:(double)duration repeat:(float)repeat
{
    if(scale<0)
    {
        scale=1.16;
    }
    
    if(duration<=0)
    {
        duration=0.6;
    }
    
    [self startAnimation:[self animationScaleXYToSize:scale withDuration:duration repeat:repeat autoreverses:YES delay:0.0] withName:kSpriteAnimationHeartbeat andDelay:0];
}


//闪烁(透明变化)(参数小于0则用默认值)
- (void)blinkWithAlpha:(float)alpha duration:(double)duration repeat:(float)repeat
{
    if(alpha<0)
    {
        alpha=0.2;
    }
    
    if(duration<=0)
    {
        duration=0.3;
    }
    
    [self startAnimation:[self animationToOpacity:alpha withDuration:duration repeat:repeat autoreverses:YES delay:0.0] withName:kSpriteAnimationBlink andDelay:0];
}


//转动(Z轴旋转)(参数小于0则用默认值)
- (void)rotateWithDuration:(double)duration repeat:(float)repeat clockwise:(BOOL)clockwise
{
    if(duration<=0)
    {
        duration=1.6;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationRotationZToAngle:clockwise?(_angleZ+M_PI*2):(_angleZ-M_PI*2) withDuration:duration repeat:repeat autoreverses:NO delay:0] withName:kSpriteAnimationRotate andDelay:0];
}


//翻转(XY轴旋转)(参数小于0则用默认值)
- (void)flipWithDuration:(double)duration repeat:(float)repeat clockwise:(BOOL)clockwise vertical:(BOOL)vertical
{
    if(duration<=0)
    {
        duration=1.6;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    if(vertical)
    {
        [self startAnimation:[self animationRotationXToAngle:clockwise?(_angleZ+M_PI*2):(_angleZ-M_PI*2) withDuration:duration repeat:repeat autoreverses:NO delay:0] withName:kSpriteAnimationRotate andDelay:0];
    }
    else
    {
        [self startAnimation:[self animationRotationYToAngle:clockwise?(_angleZ+M_PI*2):(_angleZ-M_PI*2) withDuration:duration repeat:repeat autoreverses:NO delay:0] withName:kSpriteAnimationRotate andDelay:0];
    }
}


//摇晃(左右)(参数小于0则用默认值)
- (void)shakeWithDegree:(float)degree duration:(double)duration repeat:(float)repeat
{
    if(degree<0)
    {
        degree=2.0;
    }
    
    if(duration<0)
    {
        duration=0.08;
    }
        
    [self startAnimation:[self animationWithMode:SPRITE_MODE_POSITION keyValues:@[[NSValue valueWithCGPoint:CGPointMake(self.layer.position.x-degree, self.layer.position.y)],[NSValue valueWithCGPoint:self.layer.position],[NSValue valueWithCGPoint:CGPointMake(self.layer.position.x+degree, self.layer.position.y)],[NSValue valueWithCGPoint:self.layer.position]] keyTimes:@[@(0.25),@(0.5),@(0.75),@(1.0)] duration:duration repeat:repeat autoreverses:NO delay:0] withName:kSpriteAnimationShake andDelay:0];
}


//震动(上下)(参数小于0则用默认值)
- (void)shockWithDegree:(float)degree duration:(double)duration repeat:(float)repeat
{
    if(degree<0)
    {
        degree=2.0;
    }
    
    if(duration<0)
    {
        duration=0.08;
    }
    
    
    [self startAnimation:[self animationWithMode:SPRITE_MODE_POSITION keyValues:@[[NSValue valueWithCGPoint:CGPointMake(self.layer.position.x, self.layer.position.y+degree)],[NSValue valueWithCGPoint:self.layer.position],[NSValue valueWithCGPoint:CGPointMake(self.layer.position.x, self.layer.position.y-degree)],[NSValue valueWithCGPoint:self.layer.position]] keyTimes:@[@(0.25),@(0.5),@(0.75),@(1.0)] duration:duration repeat:repeat autoreverses:NO delay:0] withName:kSpriteAnimationShock andDelay:0];
}


//抖动(旋转)(参数小于0则用默认值)
- (void)ditherWithDegree:(float)degree duration:(double)duration repeat:(float)repeat
{
    if(degree<0)
    {
        degree=2.0;
    }
    
    if(duration<0)
    {
        duration=0.1;
    }
    
    
    [self startAnimation:[self animationWithMode:SPRITE_MODE_ROTATION_Z keyValues:@[@(self.angleZ-M_PI/180.0*degree),@(self.angleZ),@(self.angleZ+M_PI/180.0*degree),@(self.angleZ)] keyTimes:@[@(0.25),@(0.5),@(0.75),@(1.0)] duration:duration repeat:repeat autoreverses:NO delay:0] withName:kSpriteAnimationDither andDelay:0];
}


//消失(透明变化)
- (void)disappearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    [self startAnimation:[self animationToOpacity:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationDisappear andDelay:0];
}


//出现(透明变化)
- (void)appearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    [self startAnimation:[self animationToOpacity:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationAppear andDelay:0];
}


//消失(缩放变化)
- (void)scaledDisappearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    [self startAnimation:[self animationScaleXYToSize:0.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationScaledDisappear andDelay:0];
}


//出现(缩放变化)
- (void)scaledAppearWithDuration:(double)duration
{
    if(duration<0)
    {
        duration=0.6f;
    }
    
    if(_sizeXY==0)
    {
        [self restoreSprite];
    }
        
    [self startAnimation:[self animationScaleXYToSize:1.0 withDuration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationScaledAppear andDelay:0];
}


//焦点出(缩放、透明变化)
- (void)zoomOutWithScale:(float)scale duration:(double)duration
{
    if(duration<0)
    {
        duration=1.38f;
    }
    
    if(scale<0)
    {
        scale=1.62;
    }
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SPRITE_MODE_SCALE_XY keyValues:@[@(1.0),@(scale)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_OPACITY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationZoomOut andDelay:0];
}


//焦点进(缩放、透明变化)
- (void)zoomInWithScale:(float)scale duration:(double)duration
{
    if(duration<0)
    {
        duration=1.38f;
    }
    
    if(scale<0)
    {
        scale=1.62;
    }
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SPRITE_MODE_SCALE_XY keyValues:@[@(scale),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_OPACITY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationZoomIn andDelay:0];
}


//滚动(旋转、位移变化)
- (void)rollToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<0)
    {
        duration=1.0;
    }
    
    float radius=(self.layer.bounds.size.width+self.layer.bounds.size.height)/2.0/2.0;//半径
    
    if(radius<=0.0)
    {
        radius=1.0;
    }
    
    double distance=[KATMath distanceWithPointA:position andPointB:self.layer.position];//距离
    BOOL clockwise=position.x>=self.layer.position.x?YES:NO;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+(clockwise?(distance/radius):-(distance/radius)) withDuration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationRoll andDelay:0];
}


//滚进(旋转、位移、透明变化)
- (void)rollInToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<0)
    {
        duration=1.0;
    }
    
    float radius=(self.layer.bounds.size.width+self.layer.bounds.size.height)/2.0/2.0;//半径
    
    if(radius<=0.0)
    {
        radius=1.0;
    }
    
    double distance=[KATMath distanceWithPointA:position andPointB:self.layer.position];//距离
    BOOL clockwise=position.x>=self.layer.position.x?YES:NO;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+(clockwise?(distance/radius):-(distance/radius)) withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_OPACITY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationRollIn andDelay:0];
}


//滚出(旋转、位移、透明变化)
- (void)rollOutToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<0)
    {
        duration=1.0;
    }
    
    float radius=(self.layer.bounds.size.width+self.layer.bounds.size.height)/2.0/2.0;//半径
    
    if(radius<=0.0)
    {
        radius=1.0;
    }
    
    double distance=[KATMath distanceWithPointA:position andPointB:self.layer.position];//距离
    BOOL clockwise=position.x>=self.layer.position.x?YES:NO;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+(clockwise?(distance/radius):-(distance/radius)) withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_OPACITY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationRollOut andDelay:0];
}


//转进(旋转、缩放、透明变化)
- (void)rotateInWithCircles:(int)circles duration:(double)duration
{
    if(duration<=0)
    {
        duration=1.38;
    }
    
    if(circles<=0)
    {
        circles=3;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SPRITE_MODE_SCALE_XY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_OPACITY keyValues:@[@(0.0),@(1.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ+M_PI*2 withDuration:duration/circles repeat:circles autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationRotateIn andDelay:0];
}


//转出(旋转、缩放、透明变化)
- (void)rotateOutWithCircles:(int)circles duration:(double)duration
{
    if(duration<=0)
    {
        duration=1.38;
    }
    
    if(circles<=0)
    {
        circles=3;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithMode:SPRITE_MODE_SCALE_XY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_OPACITY keyValues:@[@(1.0),@(0.0)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationRotationZToAngle:self.angleZ-M_PI*2 withDuration:duration/circles repeat:circles autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0] withName:kSpriteAnimationRotateOut andDelay:0];
}


//掉落
- (void)dropToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=sqrt(fabs(position.y-self.layer.position.y)*2/9.8)*0.138;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionEaseIn;
    
    //路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:self.layer.position];
    [path addQuadCurveToPoint:position controlPoint:CGPointMake(position.x, self.layer.position.y)];
    
    //角度
    float angle=atan2(position.x-self.layer.position.x, position.y-self.layer.position.y);
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithPath:path duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_ROTATION_Z keyValues:@[@(self.angleZ-angle),@(self.angleZ)] keyTimes:@[@(0.0),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationDrop andDelay:0];
}


//飘落
- (void)floatToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=fabs(position.y-self.layer.position.y)*0.012;
    }
    
    //距离
    double distance=[KATMath distanceWithPointA:position andPointB:self.layer.position];
    
    //角度
    float angle=atan2(position.x-self.layer.position.x, position.y-self.layer.position.y);
    
    //路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:self.layer.position];
    [path addCurveToPoint:position controlPoint1:CGPointMake((position.x+self.layer.position.x)/2.0-(distance/2.0/tan(M_PI/6.0)*sin(M_PI/2.0-angle)), (position.y+self.layer.position.y)/2.0-(distance/2.0/tan(M_PI/2.0-M_PI/6.0)*cos(M_PI/2.0-angle))) controlPoint2:CGPointMake((position.x+self.layer.position.x)/2.0+(distance/2.0/tan(M_PI/6.0)*sin(M_PI/2.0+angle)), (position.y+self.layer.position.y)/2.0+(distance/2.0/tan(M_PI/2.0-M_PI/6.0)*cos(M_PI/2.0-angle)))];
    
    //调整z轴
    self.layer.zPosition=self.layer.zPosition+self.layer.bounds.size.height;
    
    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationWithGroup:@[[self animationWithPath:path duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_ROTATION_Z keyValues:@[@(self.angleZ-angle),@(self.angleZ-angle+M_PI),@(self.angleZ-angle)] keyTimes:@[@(0.0),@(0.5),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0],[self animationWithMode:SPRITE_MODE_ROTATION_X keyValues:@[@(self.angleX),@(self.angleX+M_PI),@(self.angleX)] keyTimes:@[@(0.0),@(0.5),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationFloat andDelay:0];
}


//快速移动
- (void)moveQuicklyToPosition:(CGPoint)position withDegree:(float)degree duration:(double)duration
{
    if(duration<=0)
    {
        duration=sqrt([KATMath distanceWithPointA:position andPointB:self.layer.position])*0.01;
    }
    
    if(degree<0)
    {
        degree=[KATMath distanceWithPointA:position andPointB:self.layer.position]*0.06;
    }
    
    //方向
    float directionX=0.0f;
    float directionY=0.0f;
    
    if(position.x>self.layer.position.x)
    {
        directionX=1.0;
    }
    else if(position.x<self.layer.position.x)
    {
        directionX=-1.0;
    }
    
    if(position.y>self.layer.position.y)
    {
        directionY=1.0;
    }
    else if(position.y<self.layer.position.y)
    {
        directionY=-1.0;
    }
    
    self.animationTimingMode=kCAMediaTimingFunctionEaseInEaseOut;
    
    [self startAnimation:[self animationWithMode:SPRITE_MODE_POSITION keyValues:@[[NSValue valueWithCGPoint:self.layer.position],[NSValue valueWithCGPoint:CGPointMake(position.x+directionX*degree, position.y+directionY*degree)],[NSValue valueWithCGPoint:CGPointMake(position.x-directionX*degree*0.4, position.y-directionY*degree*0.4)],[NSValue valueWithCGPoint:CGPointMake(position.x+directionX*degree*0.2, position.y+directionY*degree*0.2)],[NSValue valueWithCGPoint:position]] keyTimes:@[@(0.0),@(0.55),@(0.7),@(0.85),@(1.0)] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationMoveQuickly andDelay:0];
}


//移动
- (void)moveToPosition:(CGPoint)position withDuration:(double)duration
{
    if(duration<=0)
    {
        duration=sqrt([KATMath distanceWithPointA:position andPointB:self.layer.position])*0.012;
    }
    
//    self.animationTimingMode=kCAMediaTimingFunctionLinear;
    
    [self startAnimation:[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationMove andDelay:0];
}


//移动出现
- (void)comeInToPosition:(CGPoint)position withDuration:(double)duration
{
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationToOpacity:1.0 withDuration:duration repeat:1.0 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationComeIn andDelay:0];
}


//移动消失
- (void)goOutToPosition:(CGPoint)position withDuration:(double)duration
{
    [self startAnimation:[self animationWithGroup:@[[self animationToPosition:position withDuration:duration repeat:1 autoreverses:NO delay:0.0],[self animationToOpacity:0.0 withDuration:duration repeat:1.0 autoreverses:NO delay:0.0]] duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationGoOut andDelay:0];
}


//跳跃
- (void)jumpToPosition:(CGPoint)position withHeight:(float)height andDuration:(double)duration
{
    if(height<=0)
    {
        height=fabs(position.x-self.layer.position.x)/2.0;
    }
    
    if(duration<=0)
    {
        duration=sqrt([KATMath distanceWithPointA:position andPointB:self.layer.position])*0.012+height*0.012+0.18;
    }    
    
    //抛物线顶点式方程式y=a(x-h)^2+k (k为顶点高度)
    
    //转换坐标
    float x1=position.x-self.layer.position.x;
    float y1=self.layer.position.y-position.y;
    
    //判断高度是否为零或低于y2
    if(y1>height)
    {
        height=y1;
    }
    
    //计算
    float k=height;
    float h=x1/(sqrtf(1.0-y1/k)+1.0);
    
    //生成路径
    UIBezierPath *path=[[[UIBezierPath alloc] init] autorelease];
    
    [path moveToPoint:CGPointMake(self.layer.position.x, self.layer.position.y)];
    [path addQuadCurveToPoint:CGPointMake(position.x, position.y) controlPoint:CGPointMake(self.layer.position.x+h, self.layer.position.y-k-(k-y1/2.0))];
    
    self.animationTimingMode=kCAMediaTimingFunctionEaseInEaseOut;
    
    [self startAnimation:[self animationWithPath:path duration:duration repeat:1 autoreverses:NO delay:0.0] withName:kSpriteAnimationJump andDelay:0];
}



//清除所有动画(包括当前动画和队列里的动画)
- (void)clearAnimations
{
    if(self.animationQueue)
    {
        [_animationQueue clear];
    }
    
    [self stopAnimation];
}


//停止并移除当前动画
- (void)stopAnimation
{
    //先取消暂停
    [self resumeAnimation];
    
    if(_keepStateOnStop && _animation)//停止时保持现场
    {
        CALayer *present=(CALayer *)self.layer.presentationLayer;
        
        if(present)
        {
            if(_animation)
            {                
                NSString *type=[_animation valueForKey:kSpriteKeyType];//类型
                
                if([kSpriteTypeBasic isEqualToString:type] || [kSpriteTypeKeyFrame isEqualToString:type])//基础类型,关键帧类型
                {
                    NSString *mode=[_animation valueForKey:kSpriteKeyMode];//模式
                    
                    if([kSpriteModePosition isEqualToString:mode])//位移模式
                    {
                        self.layer.position=present.position;
                    }
                    else if([kSpriteModeRotationX isEqualToString:mode])//x轴旋转模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeRotationY isEqualToString:mode])//y轴旋转模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeRotationZ isEqualToString:mode])//z轴旋转模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeScaleX isEqualToString:mode])//x轴缩放模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeScaleY isEqualToString:mode])//y轴缩放模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeScaleZ isEqualToString:mode])//z轴缩放模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeScaleXY isEqualToString:mode])//xy轴缩放模式
                    {
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                    else if([kSpriteModeOpacity isEqualToString:mode])//不透明度模式
                    {
                        self.layer.opacity=present.opacity;
                    }
                    else if([kSpriteModeBgColor isEqualToString:mode])//背景色模式(无效，不设置)
                    {
//                        self.layer.backgroundColor=present.backgroundColor;
                    }
                    else if([kSpriteModeCornerRadius isEqualToString:mode])//圆角半径模式
                    {
                        self.layer.cornerRadius=present.cornerRadius;
                    }
                    else if([kSpriteModeBorderWidth isEqualToString:mode])//边线宽模式
                    {
                        self.layer.borderWidth=present.borderWidth;
                    }
                    else if([kSpriteModeBorderColor isEqualToString:mode])//边线色模式(无效，不设置)
                    {
//                        self.layer.borderColor=present.borderColor;
                    }
                    else if([kSpriteModeContents isEqualToString:mode])//内容模式
                    {
                        self.layer.contents=present.contents;
                    }
                }
                else if([kSpriteTypePath isEqualToString:type])//路径类型
                {
                    NSString *mode=[_animation valueForKey:kSpriteKeyMode];//模式
                    
                    if([kSpriteModePosition isEqualToString:mode])//位移模式
                    {
                        self.layer.position=present.position;
                        
                        self.layer.transform=present.transform;
                        
                        //保存变换角度
                        self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                        self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                        self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                        
                        //保存变换尺寸
                        self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                        self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                        self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                        self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                    }
                }
                else if([kSpriteTypePath isEqualToString:type])//组类型
                {
                    self.layer.position=present.position;
                    self.layer.opacity=present.opacity;
                    self.layer.borderWidth=present.borderWidth;
                    self.layer.cornerRadius=present.cornerRadius;
                    self.layer.transform=present.transform;
                    self.layer.contents=present.contents;
                    
                    //保存变换角度
                    self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                    self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                    self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                    
                    //保存变换尺寸
                    self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                    self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                    self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                    self.sizeXY=(self.sizeX+self.sizeY)/2.0;
                }
            }

        }
    }
    
    
    //移除所有动画
    [self.layer removeAllAnimations];
    
    _animating=NO;
}


//保持动画结束后状态(UIView关联的Layer不会有隐式动画)
- (void)keepFinishState
{
    if(_animation)
    {
        NSString *type=[_animation valueForKey:kSpriteKeyType];//类型
        
        if([kSpriteTypeBasic isEqualToString:type] || [kSpriteTypeKeyFrame isEqualToString:type])//基础类型,关键帧类型
        {
            NSString *mode=[_animation valueForKey:kSpriteKeyMode];//模式
            
            if([kSpriteModePosition isEqualToString:mode])//位移模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKeyPosition];
                
                if(value)
                {
                    self.layer.position=[value CGPointValue];
                }
            }
            else if([kSpriteModeRotationX isEqualToString:mode])//x轴旋转模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];                    
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeRotationY isEqualToString:mode])//y轴旋转模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeRotationZ isEqualToString:mode])//z轴旋转模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeScaleX isEqualToString:mode])//x轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeScaleY isEqualToString:mode])//y轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeScaleZ isEqualToString:mode])//z轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeScaleXY isEqualToString:mode])//xy轴缩放模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            else if([kSpriteModeOpacity isEqualToString:mode])//不透明度模式
            {
                NSNumber *value=[_animation valueForKey:kSpriteKeyOpacity];
                
                if(value)
                {
                    self.layer.opacity=[value floatValue];
                }
            }
            else if([kSpriteModeBgColor isEqualToString:mode])//背景色模式
            {                
                UIColor *value=[_animation valueForKey:kSpriteKeyBgColor];
                
                if(value)
                {
                    self.backgroundColor=value;                    
                }
            }
            else if([kSpriteModeCornerRadius isEqualToString:mode])//圆角半径模式
            {
                NSNumber *value=[_animation valueForKey:kSpriteKeyCornerRadius];
                
                if(value)
                {
                    self.layer.cornerRadius=[value floatValue];
                }
            }
            else if([kSpriteModeBorderWidth isEqualToString:mode])//边线宽模式
            {
                NSNumber *value=[_animation valueForKey:kSpriteKeyBorderWidth];
                
                if(value)
                {
                    self.layer.borderWidth=[value floatValue];
                }
            }
            else if([kSpriteModeBorderColor isEqualToString:mode])//边线色模式
            {
                UIColor *value=[_animation valueForKey:kSpriteKeyBorderColor];
                
                if(value)
                {
                    self.layer.borderColor=value.CGColor;
                }
            }
            else if([kSpriteModeContents isEqualToString:mode])//内容模式
            {
                UIImage *value=[_animation valueForKey:kSpriteKeyContents];
                
                if(value)
                {
                    self.layer.contents=(__bridge id)value.CGImage;
                }
            }
        }
        else if([kSpriteTypePath isEqualToString:type])//路径类型
        {
            NSString *mode=[_animation valueForKey:kSpriteKeyMode];//模式
            
            if([kSpriteModePosition isEqualToString:mode])//位移模式
            {
                NSValue *value=[_animation valueForKey:kSpriteKeyPosition];
                
                if(value)
                {
                    self.layer.position=[value CGPointValue];
                }
            }
        }
        else if([kSpriteTypeGroup isEqualToString:type])//组类型
        {
            if([_animation valueForKey:kSpriteKeyPosition])//包含位移值
            {
                NSValue *value=[_animation valueForKey:kSpriteKeyPosition];
                
                if(value)
                {
                    self.layer.position=[value CGPointValue];
                }
            }
            
            if([_animation valueForKey:kSpriteKeyOpacity])//包含透明值
            {
                NSNumber *value=[_animation valueForKey:kSpriteKeyOpacity];
                
                if(value)
                {
                    self.layer.opacity=[value floatValue];
                }
            }
            
            if([_animation valueForKey:kSpriteKeyBgColor])//包含背景色
            {
                UIColor *value=[_animation valueForKey:kSpriteKeyBgColor];
                
                if(value)
                {
                    self.backgroundColor=value;
                }
            }
            
            if([_animation valueForKey:kSpriteKeyBorderColor])//包含边线色
            {
                UIColor *value=[_animation valueForKey:kSpriteKeyBorderColor];
                
                if(value)
                {
                    self.layer.borderColor=value.CGColor;
                }
            }
            
            if([_animation valueForKey:kSpriteKeyBorderWidth])//包含边线宽
            {
                NSNumber *value=[_animation valueForKey:kSpriteKeyBorderWidth];
                
                if(value)
                {
                    self.layer.borderWidth=[value floatValue];
                }
            }
            
            if([_animation valueForKey:kSpriteKeyCornerRadius])//包含圆角
            {
                NSNumber *value=[_animation valueForKey:kSpriteKeyCornerRadius];
                
                if(value)
                {
                    self.layer.cornerRadius=[value floatValue];
                }
            }

            if([_animation valueForKey:kSpriteKey3DTransform])//包含3d形变
            {
                NSValue *value=[_animation valueForKey:kSpriteKey3DTransform];
                
                if(value)
                {
                    self.layer.transform=[value CATransform3DValue];
                }
                
                //保存变换角度
                self.angleX=[[self.layer valueForKeyPath:@"transform.rotation.x"] floatValue];
                self.angleY=[[self.layer valueForKeyPath:@"transform.rotation.y"] floatValue];
                self.angleZ=[[self.layer valueForKeyPath:@"transform.rotation.z"] floatValue];
                
                //保存变换尺寸
                self.sizeX=[[self.layer valueForKeyPath:@"transform.scale.x"] floatValue];
                self.sizeY=[[self.layer valueForKeyPath:@"transform.scale.y"] floatValue];
                self.sizeZ=[[self.layer valueForKeyPath:@"transform.scale.z"] floatValue];
                self.sizeXY=(self.sizeX+self.sizeY)/2.0;
            }
            
            if([_animation valueForKey:kSpriteKeyContents])//包含内容
            {
                UIImage *value=[_animation valueForKey:kSpriteKeyContents];
                
                if(value)
                {
                    self.layer.contents=(__bridge id)value.CGImage;
                }
            }
        }
    }
}


#pragma -mark 代理

//动画开始
-(void)animationDidStart:(CAAnimation *)anim
{
    _animating=YES;
    _animationStoped=NO;
    self.animation=anim;
    
    if(!_interactInAnimating)
    {
        self.userInteractionEnabled=NO;//开始动画时关闭交互事件
    }
    
    if(!_restoreAfterAnimating && !anim.autoreverses)//非恢复原状
    {
        [self keepFinishState];
    }
    
    //回调
    if(_spriteDelegate && [_spriteDelegate respondsToSelector:@selector(sprite:didStartAnimation:)])
    {
        [_spriteDelegate sprite:self didStartAnimation:[anim valueForKey:kSpriteKeyName]];
    }
}



//动画结束
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    self.userInteractionEnabled=YES;//结束动画时恢复交互事件
    self.layer.beginTime=0;//恢复开始时间
    
    //飘落动画，需要调整Z轴
    if([kSpriteAnimationFloat isEqualToString:[anim valueForKey:kSpriteKeyName]])
    {
        self.layer.zPosition=self.layer.zPosition-self.layer.bounds.size.height;
    }
    
    //回调
    if(_spriteDelegate && [_spriteDelegate respondsToSelector:@selector(sprite:didStopAnimation:finished:)])
    {
        [_spriteDelegate sprite:self didStopAnimation:[anim valueForKey:kSpriteKeyName] finished:flag];
    }
    
    
    //执行队列剩下的动画
    CAAnimation *nextAnimation=[_animationQueue get];
    
    if(nextAnimation)//有动画则继续执行
    {
        if([kSpriteModePosition isEqualToString:[nextAnimation valueForKey:kSpriteKeyMode]])//包含位移动画，则在动画进行中取消交互
        {
            _interactInAnimating=NO;
        }
        
        nextAnimation.beginTime=CACurrentMediaTime();//队列里的动画，取消延迟时间
        [self.layer addAnimation:nextAnimation forKey:[nextAnimation valueForKey:kSpriteKeyName]];
    }
    else//全部执行完毕
    {
        self.animation=nil;
        _animationStoped=NO;
        _animating=NO;
    }    
}



#pragma -mark 播放(空实现，为了方便调用子类的播放动画方法)


//重新播放
- (void)replay
{
    
}


//播放（继续播放）
- (void)play
{
    
}


//停止
- (void)stop
{
    
}



#pragma -mark 重载方法


//从父视图中移除时，关闭定时器
- (void)removeFromSuperview
{
    [self stopDisplayLinkActions];
    [self clearAnimations];
    
    [super removeFromSuperview];
}


//释放内存
- (void)dealloc
{
    [self stopDisplayLinkActions];
    [self clearAnimations];
    
    if(_displayLinkActions)
    {
        Block_release(_displayLinkActions);
        _displayLinkActions=nil;
    }
    
    [_contents release];
    [_animationQueue release];
    [_animation release];
    [_animationCalculationMode release];
    [_animationRotateMode release];
    [_animationFillMode release];
    [_animationTimingMode release];
    
    [super dealloc];
}


@end
